package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.mapper.StatementItemMapper;
import cn.wolfcode.car.business.mapper.StatementMapper;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.query.StatementQuery;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class StatementServiceImpl extends ServiceImpl<StatementMapper,Statement>implements IStatementService {
    @Autowired
    private StatementMapper statementMapper;
    @Autowired
    private StatementItemMapper statementItemMapper;
    @Autowired
    private IUserService userService;

    @Override
    public TablePageInfo<Statement> query(StatementQuery qo) {
        IPage<Statement> page = new Page<>(qo.getPageNum(),qo.getPageSize());
        QueryWrapper<Statement> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(qo.getCustomerName())){
            wrapper.like("customer_name",qo.getCustomerName());
        }
        if (!StringUtils.isEmpty(qo.getCustomerPhone())){
            wrapper.like("customer_phone",qo.getCustomerPhone());
        }
        if (!StringUtils.isEmpty(qo.getLicensePlate())){
            wrapper.like("customer_phone",qo.getLicensePlate());
        }
        return new TablePageInfo<Statement>(super.page(page,wrapper));
    }

    @Override
    public void updateOtherPrice(Long userId, Long id, BigDecimal totalAmount, BigDecimal totalQuantity, BigDecimal discountPrice) {
        UpdateWrapper<Statement> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("payee_id",userId);
        wrapper.set("discount_amount",discountPrice);
        wrapper.set("total_quantity",totalQuantity);
        wrapper.set("total_amount",totalAmount);
        statementMapper.update(null,wrapper);
    }

    @Override
    @Transactional//添加事务
    public void updatePayStatementItem(Long statementItemId) {
        Statement statement = statementMapper.selectById(statementItemId);
        if (statement.getStatus().equals(Statement.STATUS_PAID)){
            throw new BusinessException("请勿重复支付");
        }
        //处于消费中才能进行支付
        if (statement.getStatus().equals(Statement.STATUS_CONSUME)){
            UpdateWrapper<Statement> wrapper = new UpdateWrapper<>();
            wrapper.eq("id",statement.getId());
            wrapper.set("status",Statement.STATUS_PAID);
            wrapper.set("pay_time",new Date());
            statementMapper.update(null,wrapper);
        }
    }

    @Override
    public Workbook exportOP(StatementItemQuery qo) {
        //获取当前客户消费信息
        Statement statement = statementMapper.selectById(qo.getStatementId());
        //获取收款人信息
        User user = userService.get(statement.getPayeeId());
        //获取服务项明细
        List<StatementItem> statementItems = statementItemMapper.selectForList(qo);
        Workbook workbook=new XSSFWorkbook();
        //创建表
        Sheet sheet = workbook.createSheet("消费明细");
        //创建行
        Row row = sheet.createRow(0);
        //创建单元格
        Cell cell1 = row.createCell(0);
        Cell cell2 = row.createCell(1);
        Cell cell3 = row.createCell(2);
        Cell cell4 = row.createCell(3);
        Cell cell5 = row.createCell(4);
        Cell cell6 = row.createCell(5);
        Cell cell7 = row.createCell(6);
        Cell cell8 = row.createCell(7);
        Cell cell9 = row.createCell(8);
        Cell cell10 = row.createCell(9);
        Cell cell11 = row.createCell(10);
        Cell cell12 = row.createCell(11);
        Cell cell13= row.createCell(12);
        Cell cell14= row.createCell(13);
        Cell cell15= row.createCell(14);
        Cell cell16= row.createCell(15);
        cell1.setCellValue("客户姓名");
        cell2.setCellValue("联系方式");
        cell3.setCellValue("车牌号码");
        cell4.setCellValue("汽车类型");
        cell5.setCellValue("服务类型");
        cell6.setCellValue("到店时间");
        cell7.setCellValue("总消费金额");
        cell8.setCellValue("服务项数量");
        cell9.setCellValue("优惠金额");
        cell10.setCellValue("实付价格");
        cell11.setCellValue("收款人");
        cell12.setCellValue("收款时间");
        cell13.setCellValue("备注信息");

        Row row1 = sheet.createRow(1);
        Cell cell21 = row1.createCell(0);
        Cell cell22 = row1.createCell(1);
        Cell cell23 = row1.createCell(2);
        Cell cell24 = row1.createCell(3);
        Cell cell25 = row1.createCell(4);
        Cell cell26 = row1.createCell(5);
        Cell cell27 = row1.createCell(6);
        Cell cell28 = row1.createCell(7);
        Cell cell29 = row1.createCell(8);
        Cell cell210 = row1.createCell(9);
        Cell cell211 = row1.createCell(10);
        Cell cell212 = row1.createCell(11);
        Cell cell213= row1.createCell(12);
        cell21.setCellValue(statement.getCustomerName());
        cell22.setCellValue(statement.getCustomerPhone());
        cell23.setCellValue(statement.getLicensePlate());
        cell24.setCellValue(statement.getCarSeries());
        cell25.setCellValue(statement.getServiceType());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String arrivalTime = simpleDateFormat.format(statement.getActualArrivalTime());
        cell26.setCellValue(arrivalTime);
        cell27.setCellValue(statement.getTotalAmount().toString());
        cell28.setCellValue(statement.getTotalQuantity().toString());
        cell29.setCellValue(statement.getDiscountAmount().toString());
        cell210.setCellValue(statement.getTotalAmount().subtract(statement.getDiscountAmount()).toString());
        cell211.setCellValue(user.getUserName());
        String payTime = simpleDateFormat.format(statement.getPayTime());
        cell212.setCellValue(payTime);
        cell213.setCellValue(statement.getInfo());
        Row row2 = sheet.createRow(2);
        row2.createCell(0).setCellValue("服务项名称");
        row2.createCell(1).setCellValue("服务项价格");
        row2.createCell(2).setCellValue("购买数量");
        //遍历信息
        StatementItem statementItem = null;
        for (int i = 0; i < statementItems.size(); i++) {
            row1=sheet.createRow(i+3);
            statementItem=statementItems.get(i);
            row1.createCell(0).setCellValue(statementItem.getItemName());
            row1.createCell(1).setCellValue(statementItem.getItemPrice().toString());
            row1.createCell(2).setCellValue(statementItem.getItemQuantity().toString());
        }
        return workbook;
    }
}
